package org.supda.system.queue;

import java.util.Comparator;
import java.util.concurrent.PriorityBlockingQueue;

import org.supda.system.entity.TMessageSend;

/**
 * 
 * @author supda
 * @Date 2017-3-10 下午3:59:56
 * 消息发送队列
 */
public class MessageQueue {

	private PriorityBlockingQueue<TMessageSend> queue = new PriorityBlockingQueue<TMessageSend>(
			10000, new Comparator<TMessageSend>() {
				public int compare(TMessageSend o1, TMessageSend o2) {
					if (o1.getTMessage().getCreateTime().before(o2.getTMessage().getCreateTime())) {
						return -1;
					} else {
						return 1;
					}
				}
			});

	private static MessageQueue instance = null;

	private MessageQueue() {}

	public static synchronized MessageQueue getInstance() {
		if (instance == null) {
			instance = new MessageQueue();
		}
		return instance;
	}

	public synchronized boolean offer(TMessageSend messageSend) {
		boolean bool = true;
		if (messageSend == null) {
			bool = false;
		} else {
			bool = this.queue.offer(messageSend);
		}
		return bool;
	}

	public synchronized TMessageSend poll() {
		TMessageSend messageSend = this.queue.poll();
		return messageSend;
	}

	public synchronized boolean remove(TMessageSend messageSend) {
		return this.queue.remove(messageSend);
	}

	public synchronized boolean isEmpty() {
		return this.queue.isEmpty();
	}

	public synchronized int getSize() {
		return this.queue.size();
	}
}
